package com.base.controller;

import com.base.helper.VosHelper;
import com.base.po.param.VosModelList;
import com.base.po.voip.Refund;
import com.base.po.voip.ShopOrder;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.*;
import com.base.utils.GsonUtil;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PQ
 * @Date 2019/8/23 9:49
 */
@Controller
@RequestMapping("/admin")
public class RefundController {
    @Autowired
    private LogsService logsService;// log日志
    @Autowired
    private RefundService refundService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private SubscribersService subscribersService;
    @Autowired
    private ShopOrderService shopOrderService;

    @RequestMapping("/refund")
    public ModelAndView index(HttpServletRequest request, Integer currentPageNo, String status,
                              String parentId, String userId, String orderId, String createTime, String orderNum,String type) throws Exception {
        Map<String, Object> map = new HashMap<>(); // 返回数据对象
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        if (adminUserId == null) {
            return new ModelAndView("jsp/refund", map);
        }
        parentId = adminUserId;
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        if (currentPageNo != null) page.setCurrentPageNo(currentPageNo); // 设置当前页
        page.setRecordTotal(refundService.countByAll(userId, orderId, parentId, createTime, status, orderNum)); // 设置总条数
        List<Refund> refunds=refundService.selectByAll(page, userId, orderId, parentId, createTime, status, orderNum);
        for (Refund r:refunds){
            String userID=r.getUserId();
            Subscribers subscriber=subscribersService.selectByPrimaryKey(userID);
                r.setUsername(subscriber.getUsername());
        }
        map.put("refundList",refunds ); // 提现集合
        map.put("status", status); // 状态
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/refund", map);
    }

    @RequestMapping("/refundSuccess")
    public @ResponseBody
    Map<String, String> success(HttpServletRequest request, String id, String userId, String orderId, String parentId) {
        Map<String, String> map = new HashMap<>();

        if (id == null || userId == null || parentId == null || orderId == null) {
            map.put("status", "400");
            map.put("msg", "数据不全");
            return map;
        }
        Users users = usersService.selectByPrimaryKey(parentId);
        Subscribers sub = subscribersService.selectByPrimaryKey(userId);
        ShopOrder order = shopOrderService.selectByPrimaryKey(orderId);
        if (parentId.equals("0b8a2f00271e40b9aea750264bf1bb58")) {           //来陶惠的ID退回购物金
            try {
                Double discount=Double.parseDouble(order.getDiscount());
                int discounti=discount.intValue();
                sub.setShopBalance(sub.getShopBalance() + discounti);
                order.setIsrefund(1);
                shopOrderService.updateByPrimaryKeySelective(order);
                refundService.updateStatus(id, 1);
                subscribersService.updateByPrimaryKey(sub);                 //更新成功，退回购物金
                map.put("status", "200");
                map.put("msg", "退款成功");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("status", "500");
                map.put("msg", "数据异常");
            }
        } else {
            String payinfo = VosHelper.PayVosAccount("{\"ownerName\":\"" + users.getAgentprefix() + sub.getMobile()
                    + "\",\"ownerType\":\"2\",\"money\":\"" + order.getDiscount() + "\",\"memo\":\"购物退款"
                    + order.getDiscount() + "元\"}");

            VosModelList v1 = GsonUtil.getObject(payinfo, VosModelList.class);

            if (v1 != null && v1.retCode == 0) {
                order.setIsrefund(1);
                shopOrderService.updateByPrimaryKeySelective(order);
                refundService.updateStatus(id, 1);
                map.put("status", "200");
                map.put("msg", "退款成功");
                return map;
            } else {
                order.setIsrefund(-1);
                refundService.updateStatus(id, -1);
                map.put("status", "500");
                map.put("msg", "退款失败");
            }
        }
        return map;
    }

    @RequestMapping("/refundFailed")
    public @ResponseBody
    Map<String, String> failed(HttpServletRequest request, String id, String orderId, String parentId) {
        Map<String, String> map = new HashMap<>();
        if (id == null || orderId == null) {
            map.put("status", "400");
            map.put("msg", "数据不全");
            return map;
        }
        refundService.updateStatus(id, 0);  //未提交审核状态
        ShopOrder order = shopOrderService.selectByPrimaryKey(orderId);
        order.setIsrefund(0);
        shopOrderService.updateByPrimaryKeySelective(order);
        map.put("status", "200");
        map.put("msg", "成功");
        return map;
    }

}
